<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\dao\car;


use app\dao\BaseDao;
use app\model\car\CarType;

/**
 * Class CarTypeDao
 * @package app\dao\shendeng
 */
class CarTypeDao extends BaseDao
{
    protected function setModel(): string
    {
        return CarType::class;
    }
    /**
     * 列表
     * @param array $where
     * @param string $field
     * @param int $page
     * @param int $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList(array $where = [], string $field = '*', int $page = 0,int $limit){
        return $this->search($where)
            ->with(['carTypeInfo','basePlace'])
            ->field($field)
            ->order('id desc')
            ->page($page, $limit)
            ->select()->toArray();
    }

    /**
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getType($where){
        return $this->search($where)
            ->with(['carTypeInfo','basePlace'])
            ->field('*')
            ->order('id desc')
            ->select()->toArray();
    }

    /**
     * @param array $where
     * @param $page
     * @param $limit
     * @return mixed
     */
    public function getCityTypeList($where = [],$page,$limit){
        $model = $this->getModel()->alias('t')->where('t.is_del','=',0);
        $model->join('car_type_info ti','ti.id=t.type_info_id','left');
        $model = $this->setWheres($model,$where);
        $model->field('t.id as type_id,t.price,t.sat_price,t.sun_price,count(ti.id) as offer,ti.type_name,ti.is_hot,ti.power,ti.gear,ti.seat_num,ti.id as ti_id,ti.cate_id,ti.pic,t.status');
        $model->group('ti.id');
        $list = $model->page($page,$limit)->select()->toArray();
        return $list;
    }

    /**
     * 查询条件
     * @param $model
     * @param $where
     * @return mixed
     */
    public function setWheres($model,$where){

        if(isset($where['place_id']) && $where['place_id']){ // 门店
            $model->where('t.place_id','in',$where['place_id']);
//            $model->where('t.place_id','in',$where['place_id']);
        }
        if(isset($where['cate_id']) && $where['cate_id']){ // 分类
            $model->where('ti.cate_id','=',$where['cate_id']);
        }
        if(isset($where['price_sort']) && $where['price_sort']){ // 排序
            if($where['price_sort'] == 1){
                $model->order('t.price asc');
            }elseif($where['price_sort'] == 2){
                $model->order('t.price desc');
            }else{
                $model->order('ti.is_hot desc');
            }
        }else{
            $model->order('ti.is_hot desc');
        }
        if(isset($where['brand_id']) && count($where['brand_id']) > 0){ // 品牌
            $brand_id = join(',',$where['brand_id']);
            $model->where('ti.brand_id','in',$brand_id);
        }
        if($where['min_price'] !== '' && $where['max_price'] !== ''){ // 价格区间
            $model->where('t.price','between',[$where['min_price'],$where['max_price']]);
        }
        if(isset($where['seat_sum']) && $where['seat_sum']){ // 座位
            $model->where('ti.seat_num','=',$where['seat_sum']);
        }
        if(isset($where['gear']) && $where['gear']){ // 档位
            $model->where('ti.gear','=',$where['gear']);
        }
        if(isset($where['status']) && $where['status']){ // 店铺车型显示
            $model->where('t.status','=',1);
        }
        return $model;
    }
}
